// uecgi_database.h
#ifndef UE_CGI_DATABASE_H
#define UE_CGI_DATABASE_H

#include "uedb.h"

class CUECGIDatabase
{
public:
	CUECGIDatabase();
	~CUECGIDatabase();

public:
	/* Create and initialize UE CGI database.
	 * @param[in] bDropExist If there is old UE CGI database exist and bDropExist is TRUE, the old database will be dropped and create a new one; 
				  if there is old UE CGI database exist but the bDropExist is FALSE, function will return FALSE.
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	static BOOL Create(_TCHAR *strBaseDir, BOOL bDropExist);

public:
	/* Connect to a UE CGI database.
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL Connect();

	/* Execute a given SQL statement for non-query purpose.
	 * @param[in] strSQL The SQL statement to be executed.
	 * @param[in] pParams The array of parameters
	 * @param[in] nParams Parameter number
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL Execute(const char *strSQL);
	BOOL ExecuteEx(const char *strSQL, const uedb_parameter* pParams, const unsigned int nParams);

	/* Execute a given SQL statement for query purpose.
	 * @param[in] strSQL The SQL statement to be executed.
	 * @return TRUE indicate success, otherwise some error occur.
	 */
	BOOL Query(const char *strSQL);

	/* Get the row number of last query result.
	 * @return Return the row number of last query result, if no any query executed before call this function, the return value is 0.
	 */
	int GetRows();

	/* Get one field value of specified row in last query result.
	 * @return Return the value of speidifed row number and column, if not find the matched row and column, return NULL.
	 */
	const char* GetField(const char* strColumnName, const int nRowNum);
	const char* GetField(const int nColumnNum, const int nRowNum);

	/* Use session id and csrf token to get authentication.
	 * @param[in] strSessionID The session ID
	 * @param[in] nCSRFToken The CSRF token
	 * @param[out] buf The buf to store the authentication
	 * @param[in] nLen The buf len
	 * @return TRUE indicate success otherwise some error occur.
	 */
	BOOL GetAuthentication(const char* strSessionID, const int nCSRFToken, char* buf, const unsigned int nLen);

	/* Close connection.
	 * @return None..
	 */
	void Close();

	/* Close last query result
	 * @return None.
	 */
	void CloseQuery();

protected:
	uedb_connection m_uedb;                              // database connection
};

#endif // UE_CGI_DATABASE_H